Updating Child Table in User Defined Object (UDO) Using SAP B1 General Service
Consider we have Main User Defined Object named Table_H and It’s child table is Table_D1
We can do using SAP B1 General Service as follows.
SAPbobsCOM.GeneralService oGeneralService = null;
SAPbobsCOM.GeneralData oGeneralData = null;
SAPbobsCOM.GeneralDataParams oGeneralParams = null;
SAPbobsCOM.CompanyService sCmp = null;
SAPbobsCOM.GeneralData oChild = null;
SAPbobsCOM.GeneralDataCollection oChildren = null;
var Company = AppCompany.GetCompanyService();
var oRs = (SAPbobsCOM.Recordset)(Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset));
string Sql =
"select DocEntry,'1' as LineId,'0' as VisOrder,'SQ_AC' as Object ,null as LogInst,'Y' as U_Checked,U_DocNum,U_SQSeries,U_SQLineId,U_iCode,U_iName,U_LineId from @SQAC_H"
oRs.DoQuery(Sql);
try
{
oGeneralService = sCmp.GetGeneralService("Table_H"); // Main UDO Name
for (int i = 1; i < 6; i++)
{
// Get UDO record
oGeneralParams =
((SAPbobsCOM.GeneralDataParams)(oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces
.gsGeneralDataParams)));
oGeneralParams.SetProperty("DocEntry", oRs.Fields.Item("DocEntry").Value); //Primary Key
oGeneralData = oGeneralService.GetByParams(oGeneralParams);
// Add lines on UDO Child Table
oChildren = oGeneralData.Child("Table_D1"); // Child Table Of Main UDO
// Create data for rows in the child table
oChild = oChildren.Add(); // Adds Blank Line in Child Table
oChild.SetProperty("U_SQLineId", oRs.Fields.Item("U_SQLineId").Value);
//oChild.SetProperty("VisOrder", oRs.Fields.Item("VisOrder").Value);
oChild.SetProperty("U_Checked", oRs.Fields.Item("U_Checked").Value);
oChild.SetProperty("U_DocNum", oRs.Fields.Item("U_DocNum").Value);
oChild.SetProperty("U_SQSeries", oRs.Fields.Item("U_SQSeries").Value);
oChild.SetProperty("U_SQLineId", oRs.Fields.Item("U_SQLineId").Value);
oChild.SetProperty("U_iCode", oRs.Fields.Item("U_iCode").Value);
oChild.SetProperty("U_iName", oRs.Fields.Item("U_iName").Value);
//Update the UDO Record
// If Child Table doesnot have any record it will create else update the existing
oGeneralService.Update(oGeneralData);
oRs.MoveNext();
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
Thanks,
Ahmed Aboalia